﻿//#define DEBUGGER

/// <summary>
/// Mob UI.cs
/// 3/11/2015
/// Gibbie Chairul
/// 
/// This script is responsible for controllling the mob.Attack this script to a mob prefab
/// </summary>


using UnityEngine;
using System.Collections;
using System;

[AddComponentMenu("Mob/All Mob Scripts")]
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(SphereCollider))]
[RequireComponent(typeof(AI))]
[RequireComponent(typeof(Movement))]

public class MobBrain : BaseCharacter {
#if DEBUGGER
	//add all of the debugging variables here
	public bool debugger = true;
#endif
	static public GameObject target;
	public string MobName;
	private Transform displayName;

	new void Awake(){
		base.Awake();
		Spawn();
	}
	// Use this for initialization
	void Start () {
		//find the cname component
		displayName = transform.FindChild("Name");
		//find the player
		target = GameObject.FindGameObjectWithTag("MainCamera"); 
		//if it does not exist warnd and return
		if(displayName == null){
			Debug.LogWarning("Please add a 3d Text to the mob");
			return;
		}

		//change the display name to what we want
		displayName.GetComponent<TextMesh>().text = MobName;
		//Debug.Log (CName);
	}

	public void DisplayHealth(){
		//for(int counter = 0; counter < Enum.GetValues(typeof(VitalName)).Length; counter++ ){
			//GetVital(counter).CurValue = GetVital(counter).AdjustedBaseValue;
//			Debug.Log(GetVital(1).CurValue +" / "+ GetVital(1).AdjustedBaseValue);
		//Messenger<int,int>.Broadcast("mob health update",GetVital(0).CurValue ,GetVital(0).AdjustedBaseValue ,MessengerMode.REQUIRE_LISTENER);
		//Messenger<bool>.Broadcast("show mob vital bars",true,MessengerMode.DONT_REQUIRE_LISTENER);
		//}
	}

	//responsible for calling the other method that need to happen when the mob first spawn
	private void Spawn(){
		//setup attributes and skills
		SetupStats();

		DisplayHealth ();
		//setup gear(Not the loot gear)
		SetupGear();
	}

	private void SetupStats(){
		for(int counter = 0; counter < Enum.GetValues(typeof(AttributeName)).Length; counter++ ){
			GetPrimaryAttribute (counter).BaseValue = UnityEngine.Random.Range(60,101);
		}
		StatUpdate();

		for(int counter = 0; counter < Enum.GetValues(typeof(VitalName)).Length; counter++ ){
			GetVital(counter).CurValue = GetVital(counter).AdjustedBaseValue;
		}

	}
	void Update(){
		//if it does not exist warnd and return
		if(displayName == null){
			Debug.LogWarning("Please add a 3d Text to the mob");
			return;
		}
		if(target == null){
			Debug.LogWarning("Cannot find the player camera");
			return;
		}
		//Make the mob names following target(Player)
		displayName.LookAt(target.transform);
		//Debug.Log (target.transform);
		displayName.Rotate(new Vector3(0,180,0));


	}
#if DEBUGGER
	//Display the mob stats only if we it targetted and the DEBUGGER
	void OnGUI(){
		if(debugger){
			int lh=20;
			for(int counter = 0; counter < Enum.GetValues(typeof(AttributeName)).Length; counter++ ){
				GUI.Label(new Rect(10,10 +(counter * lh) ,140,lh),((AttributeName)counter).ToString() + ":  " 	+GetPrimaryAttribute(counter).BaseValue);
			}

			for(int counter = 0; counter < Enum.GetValues(typeof(VitalName)).Length; counter++ ){
				GUI.Label(new Rect(10,10 +(counter * lh) +(Enum.GetValues(typeof(AttributeName)).Length * lh) ,140,lh),((VitalName)counter).ToString() + ":  " 	+GetVital(counter).CurValue+ " / " + GetVital(counter).AdjustedBaseValue );
			}
			for(int counter = 0; counter < Enum.GetValues(typeof(SkillName)).Length; counter++ ){
				GUI.Label(new Rect(310,10 +(counter * lh) ,140,lh),((SkillName)counter).ToString() + ":  " 	+GetSkill(counter).AdjustedBaseValue);
			}
		}
	}
#endif
	private void SetupGear(){
		//use the itemgenerator script to create a melee weapon
		EquipedWeapon = ItemGenerator.CreateItem( ItemType.MeleeWeapon);

		//use the itemgenerator script to create a ranged weapon

	//	Debug.Log("Name: " + _equipment[(int)EquipmentSlot.MainHand].Name);
	//	Debug.Log("Name: " + ((Weapon)_equipment[(int)EquipmentSlot.MainHand]).MaxDamage);

	}
}
